package com.lmc;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lmc.adapter.SkillsDetailsListAdapter;
import com.lmc.entity.Skills;
import com.lmc.util.GlideUtil;
import com.lmc.util.JsonManager;
import com.lmc.view.ListForScroll;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 技能详情
 */
public class SkillsDetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView name;
    private ImageView icon;
    private ListForScroll listView;
    private TextView text;
    private SkillsDetailsListAdapter adapter;
    private List<String> list = new ArrayList<>();
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skills_details);
        initView();
        id = getIntent().getStringExtra("id");
        setStatusBar();
        adapter = new SkillsDetailsListAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setFocusable(false);
        back.setOnClickListener(this);
        ActivityStack.addActivity(this);

        Get();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStack.removeActivity(this);
    }

    private void Get() {

        JSONObject params = new JSONObject();
        params.put("id", id);
        HttpRequest.getService().skillDetails(HttpRequest.getDefaultBody(params)).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = response.body().string();
                    Skills skills = JsonManager.parseJson(json, Skills.class);
                    Skills.SkillsBen ben = skills.getData().get(0);
                    GlideUtil.setImg(SkillsDetailsActivity.this, icon, ben.getIcon());
                    name.setText(ben.getName());
                    text.setText(ben.getDetails());
                    String[] split = ben.getVoice().split(",");
                    list.clear();
                    if (split != null && split.length > 0) {
                        for (String s : split) {
                            list.add(s);
                        }
                    }
                    adapter.notifyDataSetChanged();


                    Log.e("--------------", "技能详情返回:" + json);

                } catch (Exception e) {
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    /**
     * 设置导航栏：沉浸式
     */
    @TargetApi(21)
    public void setStatusBar() {
        //5.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(0);
            //4.4到5.0
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        name = (TextView) findViewById(R.id.name);
        icon = (ImageView) findViewById(R.id.icon);
        listView = (ListForScroll) findViewById(R.id.listView);
        text = (TextView) findViewById(R.id.text);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) back.getLayoutParams();
        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(this);
        back.setLayoutParams(layoutParams);
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, SkillsDetailsActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }


}
